﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_AppComCodeVsUserAppService : AsyncCrudAppService<T_Biz_AppComCodeVsUser, T_Biz_AppComCodeVsUserDto, Guid, PagedResultRequestDto, UpdateT_Biz_AppComCodeVsUserDto, UpdateT_Biz_AppComCodeVsUserDto>, IT_Biz_AppComCodeVsUserAppService
    {
        private readonly IObjectMapper _objectMapper;
        public T_Biz_AppComCodeVsUserAppService(IRepository<T_Biz_AppComCodeVsUser, Guid> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 加载客户登录密钥
        /// </summary>
        /// <param name="id">T_Biz_AppCustomer表的ID</param>
        /// <param name="cCusCode">查询条件（登陆密钥）</param>
        /// <returns></returns>
        public async Task<T_Biz_AppComCodeVsUserDto> GetLoginCode(int id,string cCusCode="")
        {
            Expression<Func<T_Biz_AppComCodeVsUser, bool>> expr = o => o.ACustomerID == id;
            if (!string.IsNullOrEmpty(cCusCode))
            {
                expr = expr.ExpressionAnd(o => o.CCusCode == cCusCode);
            }
            List<T_Biz_AppComCodeVsUser> list = new List<T_Biz_AppComCodeVsUser>();
            list = await Repository.GetAll().Where(expr).ToListAsync();
            return _objectMapper.Map<T_Biz_AppComCodeVsUserDto>(list);
        }
        /// <summary>
        /// 生成企业登录密钥
        /// </summary>
        /// <param name="id">T_Biz_AppCustomer表的ID</param>
        /// <param name="num">分配的数量</param>
        /// <param name="ncompanyID">公司ID</param>
        /// <returns></returns>
        public async Task<List<T_Biz_AppComCodeVsUserDto>> CreateLoginCode(int id,int num,int ncompanyID)
        {
            UpdateT_Biz_AppComCodeVsUserDto dto = new UpdateT_Biz_AppComCodeVsUserDto();
            List<T_Biz_AppComCodeVsUserDto> listDto = new List<T_Biz_AppComCodeVsUserDto>();
            for (int i = 1; i <= num; i++)
            {
                string str = "ES";
                //生成6位密钥随机码
                int code = GetDateRandom(1, 500000);
                string codelength = code.ToString();
                for (int j = 0; j < 5; i++)
                {
                    if (codelength.Length < 6)
                    {
                        codelength += "0";
                    }
                    else
                    {
                        codelength = codelength.Substring(0, 6);
                    }
                }
                str += codelength;
                dto.ALoginCode = str;
                dto.ACustomerID = id;
                dto.NCompanyID = ncompanyID;
                var result= await base.CreateAsync(dto);
                listDto.Add(result);
            }
            return listDto;
        }

        #region 通过DateTime.Now.Ticks 获取随机数
        /// <summary>
        /// 通过DateTime.Now.Ticks 获取随机数
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns>获取随机数</returns>
        public int GetDateRandom(int min, int max)
        {
            if (min >= max || min < 0)
                return min;
            int gapValue = max - min;
            int randomParam = (int)(DateTime.Now.Ticks % gapValue);
            return randomParam + min;
        }
        #endregion
    }
}
